using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace TestProject
{
	public class LibrarianController : Controller
	{
		public ActionResult Index ()
		{
			ViewData["Books"] = BookController.filteredBookList = BookController.searchedBooks;
			ViewData["Users"] = TestProject.User.getUsers();
			return View (ViewData);
		}
		
		public ActionResult UserAccount()
		{
			ViewData["Users"] = TestProject.User.getUsers();
			return View();
		}
		
		public ActionResult ManageUser()
		{
			ViewData["Users"] = UserController.users = TestProject.User.getUsers();
			return View();
		}
		
		public ActionResult ApproveRegistrations()
		{
			ViewData["Registrations"] = TestProject.Signup.getAllSignUp();
			ViewData["number"] = TestProject.Signup.getAllSignUp().Count;
			return View();
		}
		
		[HttpPost]
		public bool approveRegistration(int signup_ID, int level)
		{
			Signup current = new Signup(signup_ID);
			string password = generatePassword();
//			Console.WriteLine(password);
			User user = TestProject.User.addUser(current.username, password, current.firstname, current.lastname, current.email, current.contactnumber, level);
			if (user==null)
				return false;
			else{
				approvedRegistrationEmail(current.email, current.username, password);
				TestProject.Signup.deleteSignUp(signup_ID);
				return true;
			}
		}
		
		private void approvedRegistrationEmail(string emailAddress, string username, string generatedPassword)
 		{
			MailAddress fromAddress = new MailAddress("no.reply.owls@gmail.com", "OWLS Admin");
			MailAddress toAddress = new MailAddress(emailAddress, "New OWLS User");
			const string fromPassword = "OWLSPassword";
			const string subject = "OWLS: Registration Successful";
			string body = "Welcome New OWLS User,\n" +
				"\nYour registration request had been approved.\n" +
				"Your new usename is \"" + username + "\" and your temporary password is \"" + generatedPassword + "\".\n" +
				"You may now proceed to the OWLS web site at http://10.127.160.47:4040 .\n" +
				"You will be required to change your password on your next login.\n" +
				"Thank you!\n" +
				"\nOWLS Admin";
			
			SmtpClient smtp = new SmtpClient {
				Host = "smtp.gmail.com",
				Port = 587,
				EnableSsl = true,
				DeliveryMethod = SmtpDeliveryMethod.Network,
				UseDefaultCredentials = false,
				Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
			};
			using (
				MailMessage message = new MailMessage(fromAddress, toAddress) {
				     Subject = subject,
				     Body = body
				}
			) {
			    smtp.Send(message);
			}
		}
		
		[HttpPost]
		public void rejectRegistration(int signup_ID)
		{
			Signup current = new Signup(signup_ID);
			rejectRegistrationEmail(current.email);
			TestProject.Signup.deleteSignUp(signup_ID);
		}
		
		private void rejectRegistrationEmail(string emailAddress)
 		{
			MailAddress fromAddress = new MailAddress("no.reply.owls@gmail.com", "OWLS Admin");
			MailAddress toAddress = new MailAddress(emailAddress, "New OWLS User");
			const string fromPassword = "OWLSPassword";
			const string subject = "OWLS: Registration Rejected";
			string body = "Hi,\n" +
				"\nYour registration request has been rejected.\n" +
				"Please contact the librarian for more information.\n" +
				"Thank you!\n" +
				"\nOWLS Admin";
			
			SmtpClient smtp = new SmtpClient {
				Host = "smtp.gmail.com",
				Port = 587,
				EnableSsl = true,
				DeliveryMethod = SmtpDeliveryMethod.Network,
				UseDefaultCredentials = false,
				Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
			};
			using (
				MailMessage message = new MailMessage(fromAddress, toAddress) {
				     Subject = subject,
				     Body = body
				}
			) {
			    smtp.Send(message);
			}
		}
		
		public string generatePassword()
		{
			Random random = new Random((int)DateTime.Now.Ticks);
			StringBuilder builder = new StringBuilder();
	        char ch;
	        for (int i = 0; i < 8; i++)
	        {
	            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));                 
	            builder.Append(ch);
	        }	
	        return builder.ToString();
		}
		
		public ActionResult Unreturned()
		{
			ViewData["unreturned"] = Book.getUnreturnedBooks();
			return View();
		}
	}
}

